package com.hanxiaozhang.base.controller;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.hanxiaozhang.base.entity.LogEntity;
import com.hanxiaozhang.base.service.LogService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * 系统日志
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-04-08
 */
@Slf4j
@Controller
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogService logService;

    /**
     * 查询列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('base:log:list')")
    public Result<PageUtil> list(@RequestParam Map<String, Object> params) {

        Page<LogEntity> page = PageHelper
                .startPage(Integer.parseInt(params.get("current").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<LogEntity> logList = logService.list(params);
        return Result.success(new PageUtil(logList, (int) page.getTotal()));
    }


    /**
     * 保存
     *
     * @param logEntity
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Void> save(@RequestBody LogEntity logEntity) {

        logService.save(logEntity);
        return Result.success();
    }


    /**
     * 删除
     *
     * @param logId
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:log:remove')")
    public Result<Void> remove(Long logId) {

        logService.remove(logId);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @param logIds
     * @return
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:log:batchRemove')")
    public Result<Void> remove(Long[] logIds) {

        logService.batchRemove(logIds);
        return Result.success();
    }

}
